package com.example.hyw.szysteam.view;


import android.os.Bundle;
import android.support.annotation.Nullable;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.adapter.MyFragmentPagerAdapter;
import com.example.hyw.szysteam.utils.ActivityManager;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;


import java.util.ArrayList;

/**
 * Created by HYW on 2018/5/22.
 */

public class ViewPagerActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentList=new ArrayList<>();
    private Fragment fragment1,fragment2,fragment3;
    private MyFragmentPagerAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        ActivityManager.addDestoryActivity(this,"viewPagerActivity");
        initView();
    }

    private void initView(){
        tabLayout=findViewById(R.id.tabs);
        viewPager=findViewById(R.id.viewpager);
        fragment1=new HomePagerFragmrnt();
        fragment2=new CommunityFragment();
        fragment3=new MineFragment();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        adapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_home_selected));
        tabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_community));
        tabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_mine));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab==tabLayout.getTabAt(0)){
                    tabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_home_selected));
                }else if(tab==tabLayout.getTabAt(1)){
                    tabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_community_selected));
                }else if(tab==tabLayout.getTabAt(2)){
                    tabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_mine_selected));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if(tab==tabLayout.getTabAt(0)){
                    tabLayout.getTabAt(0).setIcon(getResources().getDrawable(R.drawable.ic_home));
                }else if(tab==tabLayout.getTabAt(1)){
                    tabLayout.getTabAt(1).setIcon(getResources().getDrawable(R.drawable.ic_community));
                }else if(tab==tabLayout.getTabAt(2)){
                    tabLayout.getTabAt(2).setIcon(getResources().getDrawable(R.drawable.ic_mine));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
